package cn.wolfcode.utils;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    private static final String EMPLOYEE_IN_SESSION ="EMPLOYEE_IN_SESSION";
    private static final String EXPRESSIONS_IN_SESSION ="EXPRESSIONS_IN_SESSION";

    //获取请求对象
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

    //获取session对象
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    public static void setEmployee(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    public static Employee getEmployee(){
        return (Employee)getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    public static void setExpressions(List<String> expressions){
        getSession().setAttribute(EXPRESSIONS_IN_SESSION,expressions);
    }

    public static List<String> getExpressions(){
        return (List<String>)getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }

    public static void setMainMenu(List<String> expressions){
        getSession().setAttribute(EXPRESSIONS_IN_SESSION,expressions);
    }

}

